package cn.kgc.tangcco.respose;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.kgc.tangcco.utils.servlet.BaseServlet;
import com.google.code.kaptcha.servlet.KaptchaServlet;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/10/14 10:23
 */
@WebServlet(urlPatterns = "/response.action")
public class ResposeAction extends BaseServlet {
    private static final long serialVersionUID = -7679470376235747188L;

    public void print(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 处理中文乱码
        // 处理post请求的中文乱码
        request.setCharacterEncoding("utf-8");
        // 处理响应的字符集中文乱码
        response.setCharacterEncoding("utf-8");
        // 处理浏览器字符集中文乱码
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print("桃李不言下自成蹊！");
        writer.flush();
        writer.close();
    }

    public void printText(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 处理中文乱码
        // 处理post请求的中文乱码
        request.setCharacterEncoding("utf-8");
        // 处理响应的字符集中文乱码
        response.setCharacterEncoding("utf-8");
        // 处理浏览器字符集中文乱码
        response.setContentType("text/html;charset=utf-8");
        printText(response, "桃李不言下自成蹊！");
    }

    public void printQrCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取servlet字节输出流
        ServletOutputStream out = response.getOutputStream();
        QrConfig config = new QrConfig(300, 300);
        // 设置边距，既二维码和背景之间的边距
        config.setMargin(3);
        // 设置前景色，既二维码颜色（青色）
        config.setForeColor(Color.CYAN.getRGB());
        // 设置背景色（灰色）
        config.setBackColor(Color.GRAY.getRGB());
        // 获取logo存储路径
        String path = this.getServletContext().getRealPath("res/logo/head.png");
        // 设置logo图标的路径
        config.setImg(path);
        // 生成二维码文件
        QrCodeUtil.generate("http://www.ekgc.cn", config, "png", out);
        out.flush();
        out.close();
    }

    public void qrCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取servlet字节输出流
        ServletOutputStream out = response.getOutputStream();
        // 获取logo存储路径
        String path = this.getServletContext().getRealPath("res/logo/head.png");
        // 设置logo图标的路径
        // 生成二维码文件
        QrCodeUtil.generate("http://www.ekgc.cn", QrConfig.create().setImg(path), "png", out);
        out.flush();
        out.close();
    }

    public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(84, 40, 4, 2);
        //图形验证码写出，可以写出到文件，也可以写出到流
        // captcha.write("d:/shear.png");
        captcha.write(response.getOutputStream());
    }
}
